{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4248761",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "BASE_URL = \"https://cmpdc.iphy.ac.cn/topomat-api\"  # 根据实际地址修改\n",
    "# BASE_URL = \"http://localhost:8000/\"  # 本地测试地址\n",
    "POSCAR = \"\"\"5yOhtAoR\n",
    "1.0\n",
    "        5.6649999619         0.0000000000         0.0000000000\n",
    "       -2.8324999809         4.9060338794         0.0000000000\n",
    "        0.0000000000         0.0000000000         4.5310001373\n",
    "   Mn   Sn\n",
    "    6    2\n",
    "Direct\n",
    "     0.838800013         0.677600026         0.250000000  1.5 2.5981 0\n",
    "     0.161199987         0.838800013         0.750000000  -3   0     0\n",
    "     0.838800013         0.161199987         0.250000000  -3   0     0\n",
    "     0.161199987         0.322399974         0.750000000  1.5 2.5981 0\n",
    "     0.322400004         0.161200002         0.250000000  1.5 -2.5981 0\n",
    "     0.677600026         0.838800013         0.750000000  1.5 -2.5981 0\n",
    "     0.333332986         0.666666985         0.250000000\n",
    "     0.666666985         0.333333999         0.750000000\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "TQC_DATA = \"\"\"88    5   64\n",
    "  1 11 16  9 14 10 12 13 15  9 12 10 11 14 15 13 16 11  9 12 10  9 11  9 12 11 10 11 12 10  9 12 10  9 12 11 10 15 16 11 12  9 16 13 14 15 10 11  9 10 14 12 11 12 10 11 12  9  9 12 11 10  9 16 13\n",
    "  2  4  3  3  4  3  4  3  4  4  3  4  3  4  3  3  4  3  4  4  3  4  3  3  3  4  4  4  4  3  4  3  4\n",
    "  4  5  8  6  8  7  5  6  7  6  8  5  7  6  8  7  5  7  6  7  5  8  5  5  8  6  8  7  6  6  8  5  7  5  7  8  6  6  7  5  8  6  7  8  5  7  5  6  8  5  8  6  7  8  5  6  6  7  5  7  8  6  7  8  6\n",
    "  5  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2  2\n",
    "  8  4  3  4  3  4  3  4  3  4  4  3  4  3  3  4  3  3  3  4  4  3  3  3  3  3  3  3  3  3  3  3  4  4  3  3  3  4  3  3  3  4  3  4  4  4  4  4  3  4  3  4  4  4  3  3  3  3  3  3  3  3  3  4  3\n",
    "\"\"\"\n",
    "\n",
    "# POSCAR = \"\"\"SG 164  0.000  0.000  0.000 :Generated by spacegroup for irvsp!\n",
    "#     1.0\n",
    "#      0.000000   0.707107   0.707107\n",
    "#      0.707107   0.000000   0.707107\n",
    "#      0.707107   0.707107   0.000000\n",
    "#  B As\n",
    "#  1 1\n",
    "# Direct\n",
    "#   0.0000000  0.0000000  0.0000000\n",
    "#   0.7500000  0.7500000  0.7500000\n",
    "# \"\"\"\n",
    "\n",
    "# TQC_DATA = \"\"\"216    4    6\n",
    "#   1  4  4\n",
    "#   2  5  1  5  3\n",
    "#   3  3  1  3  1\n",
    "#   6  4  2  1  3  2  4\n",
    "# \"\"\"\n",
    "\n",
    "\n",
    "def print_json(data):\n",
    "    \"\"\"\n",
    "    美化打印JSON数据，并将字符串值中的 \\\\n 转换成实际换行进行显示。\n",
    "    \"\"\"\n",
    "    # 首先，使用 json.dumps 进行格式化，indent 参数产生结构上的换行\n",
    "    # ensure_ascii=False 保证中文等非ASCII字符正常显示\n",
    "    json_string = json.dumps(data, indent=2, ensure_ascii=False)\n",
    "\n",
    "    # json.dumps 会将字符串中的实际换行符 \\n 转换成JSON字符串中的 \\\\n\n",
    "    # 例如，Python 字符串 \"line1\\nline2\" 会变成JSON字符串 \"...\\\"line1\\\\nline2\\\"...\"\n",
    "    # 当打印这个JSON字符串时，\\\\n 会显示为字面上的 \\n\n",
    "    # 为了让其显示为实际换行，我们在这里进行替换\n",
    "    # 注意：这也可能影响那些本意就是字面量 '\\\\n' 的字符串\n",
    "    processed_string = json_string.replace(\"\\\\n\", \"\\n\")\n",
    "    processed_string = processed_string.replace(\"\\\\\\\\\", \"\\\\\")\n",
    "    print(processed_string)\n",
    "\n",
    "\n",
    "def pos2abr() -> dict:\n",
    "    payload = {\"poscar_content\": POSCAR}\n",
    "    resp = requests.post(f\"{BASE_URL}/pos2abr\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def ebr_decomp() -> dict:\n",
    "    payload = {\"poscar_content\": POSCAR, \"tqc_data\": TQC_DATA}\n",
    "    resp = requests.post(f\"{BASE_URL}/ebr_decomp\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def abr_decomp() -> dict:\n",
    "    payload = {\"poscar_content\": POSCAR, \"tqc_data\": TQC_DATA}\n",
    "    resp = requests.post(f\"{BASE_URL}/abr_decomp\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def solve_cr(og_number: int | None = None) -> dict:\n",
    "    payload = {\"tqc_data\": TQC_DATA, \"og_number\": og_number}\n",
    "    resp = requests.post(f\"{BASE_URL}/solve_cr\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def pos2msg(sg_number: int, og_number: int) -> dict:\n",
    "    payload = {\n",
    "        \"poscar_content\": POSCAR,\n",
    "        \"sg_number\": sg_number,\n",
    "        \"og_number\": og_number,\n",
    "    }\n",
    "    resp = requests.post(f\"{BASE_URL}/pos2msg\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def mom2msg() -> dict:\n",
    "    payload = {\"poscar_content\": POSCAR}\n",
    "    resp = requests.post(f\"{BASE_URL}/mom2msg\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()\n",
    "\n",
    "\n",
    "def cal_si(og_number: int) -> dict:\n",
    "    payload = {\n",
    "        # \"poscar_content\": POSCAR,\n",
    "        \"tqc_data\": TQC_DATA,\n",
    "        \"og_number\": og_number,\n",
    "    }\n",
    "    resp = requests.post(f\"{BASE_URL}/cal_si\", json=payload)\n",
    "    resp.raise_for_status()\n",
    "    return resp.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d6cd84b",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    # print_json(pos2abr())\n",
    "    # print_json(ebr_decomp())\n",
    "    # print_json(abr_decomp())\n",
    "    # print_json(solve_cr())\n",
    "    # print_json(pos2msg(141, 1219))\n",
    "    print_json(mom2msg())\n",
    "    # print_json(solve_cr(1219))\n",
    "    # print_json(cal_si(1219))\n",
    "except requests.exceptions.HTTPError as err:\n",
    "    print_json(err.response.json())\n",
    "\n",
    "# 9bea43a33c4aea49227adb81566add7fb877b477792b2cca971a0faa4bb14c32\n",
    "# 1. mom2msg -> poscar_msg\n",
    "# 2. poscar_msg中读取sg og(以及磁矩信息)\n",
    "# 3. main() -> POSCAR_clean\n",
    "# 4. POSCAR_clean -> pos2msg(sg, og) -> result\n",
    "# 5. result中的poscar_msg执行remove_He\n",
    "# 6. parse_poscar(POSCAR_clean) -> mag, frac_coord, latt, species, counts\n",
    "# 7. 将mag替换输出中的incar.MAGMOM"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
